package com.leo.encryptiondemo;

import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;

import static android.R.attr.path;
import static android.R.attr.pathData;

public class MainActivity extends AppCompatActivity {

    private Handler mHandler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){

                case 1:

                    byte[] res= (byte[]) msg.obj;
                    Toast.makeText(MainActivity.this, new String("asdasdasdas"), Toast.LENGTH_SHORT).show();
                    break;


            }
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initData();
    }
    String path="http://api.m.mtime.cn/Showtime/LocationMovies.api?locationId=";

    private void initData() {
        try {
            String encode = URLEncoder.encode("%", "UTF-8");
            path=path+encode+"s";
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        new Thread(new Runnable() {
            @Override
            public void run() {
                InputStream inputStream=null;
                ByteArrayOutputStream baos=null;
                try {
                    URL url=new URL(path);

                    HttpURLConnection conn= (HttpURLConnection) url.openConnection();

                    if(conn.getResponseCode()==200){

                        inputStream = conn.getInputStream();

                        byte[] bt=new byte[1024*8];
                        int len=0;

                        while((len=inputStream.read(bt))!=-1){

                            baos.write(bt,0,len);


                        }
                        Log.d("flag", "----------->得到的数据为run:" +new String(baos.toByteArray()));
                        mHandler.sendMessage(Message.obtain(mHandler,1,baos.toByteArray()));


                    }



                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }finally {
                    if (baos != null) {
                        try {
                            baos.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    if (inputStream != null) {
                        try {
                            inputStream.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }

            }
        }).start();


    }
}
